package com.tianci.appstore.ui.list.manage;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.tianci.appstore.R;
import com.tianci.appstore.audio.IAudio;
import com.tianci.appstore.audio.IAudioMatchListener;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyListAppData;
import com.tianci.appstore.ui.data.SkyListAppData.ListType;
import com.tianci.appstore.ui.list.ListBaseView;
import com.tianci.appstore.ui.list.ListBlock;
import com.tianci.appstore.ui.list.ListFlipper;
import com.tianci.appstore.ui.list.SkyListAdapter;
import com.tianci.appstore.ui.list.SkyListAdapter.OnItemClickListener;
import com.tianci.appstore.ui.list.SkyListAdapter.SelectableListener;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.ui.widget.SkyAppPager.Direction;
import com.tianci.appstore.ui.widget.SkyCustomLayout;
import com.tianci.appstore.ui.widget.SkyCustomLayout.BlockData;
import com.tianci.appstore.ui.widget.SkyCustomLayout.CustomLayoutParams;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public abstract class ManageBaseListView extends ListBaseView
        implements OnItemClickListener, SelectableListener, IAudioMatchListener {
    protected Context mContext;

    protected static final int UPDATE_LIST = 0;
    protected static final int UPDATE_STATUS = 1;
    protected static final int UPDATE_PERCENT = 2;
    protected static final int DELAY_DISSMISS_DIALOG = 3;
    protected static final int UPDATE_SUCCESS_STATE = 4;
    protected static final int UPDATE_FAILED_STATE = 5;

    protected int lastFocusPageIndex = -1;
    protected int lastFocusIndex = -1;

    protected TextView titleText;
    protected ListFlipper listFlipper;
    protected TextView pageNumberText;
    protected ImageView leftArrow, rightArrow;

    protected ManageListController mController;

    protected ListType listType;

    protected static int titleSize = SkyConstants.Dpi(42);
    protected static int rightTopTextSize = SkyConstants.Dpi(39);

    protected int recoverPageIndex = 0;     // 列表出现变动时，刷新数据之后，UI显示第几页

    public ManageBaseListView(Context context, ListType listType) {
        super(context);
        this.mContext = context;
        this.listType = listType;
        createView();
    }

    private void createView() {
        SkinManager.setBackground(this, R.drawable.ui_sdk_app_bg);

        initTitle();

        initFlipper();
        initPageNoView();
    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }

    public IAudio getAudioInterface() {
        return mController;
    }

    protected void initTitle() {
        titleText = UICommon.newTextView(mContext, titleSize, R.color.titleColor);
        LayoutParams titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        titleParams.setMargins(SkyConstants.Div(102), SkyConstants.Div(85), 0, 0);
        this.addView(titleText, titleParams);

        switch (listType) {
            case UPGRADE_APP_LIST:
                titleText.setText(R.string.app_upgrade);
                break;
            case DOWNLOAD_LIST:
                titleText.setText(R.string.download_manage);
                break;
            case UNINSTALL_APP_LIST:
                titleText.setText(R.string.delete_app);
                break;
            case MOVE_APP_LIST:
                titleText.setText(R.string.move_app);
                break;
            case CLEAR_CACHE_LIST:
                titleText.setText(R.string.clear_cache);
                break;
            case LOCAL_APP_LIST:
                titleText.setText(R.string.my_app);
                break;
            case APK_SCAN_LIST:
                titleText.setText(R.string.apk_search);
                break;
            case LAUNCH_ON_BOOT_LIST:
                titleText.setText(R.string.launch_on_boot_setting);
                break;
            case SELECTED_LAUNCH_LIST:
                titleText.setText(R.string.select_launch_app);
                break;
            default:
                break;
        }
    }


    protected void initFlipper() {
        int row = 3, column = 4;
        listFlipper = new ListFlipper(mContext, Direction.HORIZONTAL);
        mController = new ManageListController(mContext, listFlipper, row, column);
        mController.setAudioMatchListener(this);
        List<BlockData> blockDataList = createListBlockData(row, column);
        for (int i = 0; i < ListFlipper.VIEW_PAGE_COUNT; i++) {
            SkyCustomLayout page = new SkyCustomLayout(mContext, CoocaaApplication.mDiv);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            page.setLayoutParams(params);
            CustomLayoutParams netAppParams = new CustomLayoutParams();
            netAppParams.mRow = row;
            netAppParams.mCol = column;
            netAppParams.mBlockWidth = SkyConstants.Div(472);
            netAppParams.mBlockHeight = SkyConstants.Div(280);
            netAppParams.mHorizontalSpace = SkyConstants.Div(-47);
            netAppParams.mVerticalSpace = SkyConstants.Div(-36);
            netAppParams.mMarginTop = SkyConstants.Div(202);
            netAppParams.mMarginLeft = SkyConstants.Div(97);
            page.setCustomLayoutParams(netAppParams);
            page.setBlockDatas(blockDataList);
            SkyListAdapter mAdapter = new SkyListAdapter(mContext, listType, i, CoocaaApplication.mDiv);
            mAdapter.setKeyListener(mController);
            mAdapter.setOnItemClickListener(this);
            mAdapter.setAudioMatchListener(this);
            mAdapter.setMultiSelectListener(mController);
            mAdapter.setItemSelectableListener(this);
            page.setAdapter(mAdapter);
            listFlipper.addView(page);
        }
        contentLayout.addView(listFlipper, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL));
    }

    protected List<BlockData> createListBlockData(int row, int column) {
        List<BlockData> datas = new ArrayList<BlockData>();
        for (int i = 0; i < row * column; i++) {
            BlockData data = new BlockData();
            data.mColNum = 1;
            data.mRowNum = 1;
            data.mBlock = new int[]{i};
            boolean leftTurn = false;
            boolean rightTurn = false;
            if (i % column == 0)
                leftTurn = true;
            if (i % column == column - 1)
                rightTurn = true;
            data.mTurnPage = new boolean[]{leftTurn, false, rightTurn, false};
            datas.add(data);
        }
        return datas;
    }

    private void initPageNoView() {
        leftArrow = new ImageView(mContext);
        rightArrow = new ImageView(mContext);
        leftArrow.setImageDrawable(SkinManager.getResDrawable(R.drawable.left_arrow));
        rightArrow.setImageDrawable(SkinManager.getResDrawable(R.drawable.right_arrow));
        LayoutParams leftParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.LEFT);
        LayoutParams rightParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.RIGHT);
        leftParams.leftMargin = SkyConstants.Div(36);
        rightParams.rightMargin = SkyConstants.Div(36);

        contentLayout.addView(leftArrow, leftParams);
        contentLayout.addView(rightArrow, rightParams);

        pageNumberText = UICommon.newTextView(mContext, SkyConstants.Dpi(33), R.color.pageNumberColor);
        LayoutParams pageParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
        pageParams.bottomMargin = SkyConstants.Div(45);
        contentLayout.addView(pageNumberText, pageParams);
        mController.setPageNoView(pageNumberText, leftArrow, rightArrow);
    }

    protected void resetFocus() {
        SkyCustomLayout child = (SkyCustomLayout) listFlipper.getChildAt(listFlipper.getDisplayedChild());
        if (child != null)
            child.setFocusPos(0, true);
    }


    protected void setInSelectMode(boolean selMode) {
        for (int i = 0; i < listFlipper.getChildCount(); i++) {
            SkyCustomLayout child = (SkyCustomLayout) listFlipper.getChildAt(i);
            for (View v : child.getChildViews()) {
                ListBlock block = (ListBlock) v;
                block.setSelectMode(selMode);
            }
            SkyListAdapter mAdapter = (SkyListAdapter) child.getAdapter();
            mAdapter.setInSelectMode(selMode);
        }
    }

    protected void setBtnEnabled(Button btn, boolean enabled) {
        if (btn == null)
            return;
        if (enabled)
            btn.setAlpha(1.0f);
        else
            btn.setAlpha(UICommon.disableAlpha);
        btn.setEnabled(enabled);
        btn.setFocusable(enabled);
    }

    @Override
    public boolean checkSelectable(SkyListAppData data, int index) {
        return true;
    }

    private SkyCustomLayout getPageByIndex(int index) {
        return (SkyCustomLayout) listFlipper.getChildAt(index);
    }

    @Override
    public void destroyController() {
        try {
            if (listFlipper != null) {
                int count = listFlipper.getChildCount();
                for (int i = 0; i < count; i++) {
                    getPageByIndex(i).getAdapter().destory();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemAudioMatch(SkyAppBaseData data, int index) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAudioMatch(String name) {
        // TODO Auto-generated method stub

    }

    @Override
    public HashMap<String, String[]> addExtraCommand(HashMap<String, String[]> current) {
        return current;
    }

}
